package com.demo.controller;

import com.demo.dto.CourseDTO;
import com.demo.dto.CoursePageQueryDTO;
import com.demo.result.PageResult;
import com.demo.result.Result;
import com.demo.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin(origins = "http://localhost:8080")
@RequestMapping("/instructor/course")
public class CourseController {
    @Autowired
    CourseService courseService;

    @PostMapping("/page")
    public Result<PageResult> page(@RequestBody CoursePageQueryDTO coursePageQueryDTO){
        PageResult pageResult=courseService.pageQuery(coursePageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping()
    public Result save(@RequestBody CourseDTO courseDTO){
        System.out.println(courseDTO);
        courseService.save(courseDTO);
        return Result.success();
    }

    /**
     * 批量删除
     * @param courseIDs
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestBody String[] courseIDs){
        System.out.println(courseIDs);
        courseService.delete(courseIDs);
        return Result.success();
    }

    /**
     * 单个删除
     * @param courseID
     * @return
     */
    @DeleteMapping("/id")
    public Result deleteById(@RequestParam("courseID") String courseID){
        System.out.println(courseID+"删除");
        courseService.deleteById(courseID);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody CourseDTO courseDTO){
        System.out.println(courseDTO);
        courseService.update(courseDTO);
        System.out.println("修改成功");
        return Result.success();
    }
}
